package com.yinhai.message.implement.dwr.util;

import java.util.List;

import org.directwebremoting.Browser;
import org.directwebremoting.ScriptBuffer;
import org.directwebremoting.ScriptSession;
import org.directwebremoting.ScriptSessionFilter;

import com.yinhai.message.api.bean.TMUser;

/**
 * 服务器推送消息工具类
 * @author maxp
 * @Create 2017-01-04-16:23
 */
public class SendMessageAutoUtil {

    /**
     * 消息推送给一个客户端
     * @param user  用户
     * @param message  消息
     * @param showScriptMethod  客户端调用js
     */
    public static void sendMessageAuto(final TMUser user, final Object message, final String showScriptMethod){
        Browser.withAllSessionsFiltered(new ScriptSessionFilter() {
            @Override
            public boolean match(ScriptSession session) {
                String user_Id = (String) session.getAttribute("userid");
                String app_Id = (String) session.getAttribute("systemid");
                String page_Id = (String) session.getAttribute("pageid");
                if(user_Id != null && app_Id != null){
                    String p = user.getPageId();
                    if(app_Id.equals(user.getAppId()) && user_Id.equals(user.getUserId()) && (p==null || p.equals(page_Id))){
                    	user.setOnline("1");
                    	return true;
                    }
                }
                return false;
            }
        }, new Runnable() {
            private ScriptBuffer script = new ScriptBuffer();
            @Override
            public void run() {
                script.appendCall(showScriptMethod,message);//设置调用的方法和参数
                for(ScriptSession scriptSession:Browser.getTargetSessions()){ //获取过滤后的ScriptSession
                    scriptSession.addScript(script);
                }
            }
        });
    }

    /**
     * 消息推送给多个客户端
     * @param userList 用户列表
     * @param message   消息内容
     * @param showScriptMethod  调用的前端方法
     */
    public static void sendMessageAuto(final List<TMUser> userList, final Object message, final String showScriptMethod){
        Browser.withAllSessionsFiltered(new ScriptSessionFilter() {
            @Override
            public boolean match(ScriptSession session) {
                String user_Id = (String) session.getAttribute("userid");
                String app_Id = (String) session.getAttribute("systemid");
                String page_Id = (String) session.getAttribute("pageid");
                if(user_Id != null && app_Id != null){
                	for(TMUser user: userList){
                	    String p = user.getPageId();
                		 if(app_Id.equals(user.getAppId()) && user_Id.equals(user.getUserId()) && (p==null || p.equals(page_Id)) ){
                         	user.setOnline("1");
                         	return true;
                         }
                    }
                }
                return false;
            }
        }, new Runnable() {
            private ScriptBuffer script = new ScriptBuffer();
            @Override
            public void run() {
                script.appendCall(showScriptMethod,message);
                for(ScriptSession scriptSession:Browser.getTargetSessions()){
                    scriptSession.addScript(script);
                }
            }
        });
    }

}
